/*
 * @Author: szx
 * @Date: 2022-03-13 10:38:47
 * @LastEditTime: 2022-03-13 11:14:47
 * @Description:
 * @FilePath: \leetcode\5200-5299\5203\5203.js
 */
/**
 * @param {number} n
 * @param {number[][]} artifacts
 * @param {number[][]} dig
 * @return {number}
 */
var digArtifacts = function (n, artifacts, dig) {
    const cnt = new Array(artifacts.length).fill(0);
    const map = new Map();
    for (let i = 0; i < artifacts.length; i++) {
        const r1 = artifacts[i][0];
        const c1 = artifacts[i][1];
        const r2 = artifacts[i][2];
        const c2 = artifacts[i][3];
        for (let r = r1; r <= r2; r++) {
            for (let c = c1; c <= c2; c++) {
                map.set(r + '.' + c, i);
                cnt[i] += 1;
            }
        }
    }
    let res = 0;
    for (let di of dig) {
        const d = di[0] + '.' + di[1];
        if (!map.has(d)) continue;
        const idx = map.get(d);
        map.delete(d);
        cnt[idx] -= 1;
        if (cnt[idx] === 0) res += 1;
    }
    return res;
};
console.log(
    digArtifacts(
        2,
        [
            [0, 0, 0, 0],
            [0, 1, 1, 1]
        ],
        [
            [0, 0],
            [0, 1]
        ]
    )
);
console.log(
    digArtifacts(
        2,
        [
            [0, 0, 0, 0],
            [0, 1, 1, 1]
        ],
        [
            [0, 0],
            [0, 1],
            [1, 1]
        ]
    )
);

console.log(
    digArtifacts(
        6,
        [
            [0, 2, 0, 5],
            [0, 1, 1, 1],
            [3, 0, 3, 3],
            [4, 4, 4, 4],
            [2, 1, 2, 4]
        ],
        [
            [0, 2],
            [0, 3],
            [0, 4],
            [2, 0],
            [2, 1],
            [2, 2],
            [2, 5],
            [3, 0],
            [3, 1],
            [3, 3],
            [3, 4],
            [4, 0],
            [4, 3],
            [4, 5],
            [5, 0],
            [5, 1],
            [5, 2],
            [5, 4],
            [5, 5]
        ]
    )
);

console.log(
    digArtifacts(
        8,
        [
            [3, 2, 6, 2],
            [2, 7, 3, 7],
            [1, 2, 1, 5],
            [4, 4, 4, 7],
            [0, 0, 3, 0],
            [5, 6, 7, 6],
            [0, 1, 0, 3],
            [6, 0, 7, 1],
            [2, 1, 4, 1],
            [2, 4, 2, 4],
            [5, 5, 5, 5],
            [5, 3, 7, 3],
            [1, 1, 1, 1],
            [0, 7, 1, 7]
        ],
        [
            [0, 0],
            [0, 1],
            [0, 3],
            [0, 4],
            [0, 5],
            [0, 6],
            [1, 4],
            [1, 6],
            [2, 2],
            [2, 4],
            [2, 5],
            [3, 0],
            [3, 1],
            [3, 2],
            [3, 5],
            [3, 6],
            [4, 0],
            [4, 1],
            [4, 2],
            [4, 6],
            [5, 1],
            [5, 2],
            [5, 4],
            [5, 7],
            [6, 0],
            [6, 3],
            [6, 4],
            [6, 6],
            [7, 5]
        ]
    )
);
